package cn.myzqu.sign.gateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public class AccessFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(AccessFilter.class);


    @Override
    public String filterType() {
        //过滤类型
        return "pre";
    }

    @Override
    public int filterOrder() {
        //过滤执行器顺序
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        //是否过滤
        return true;
    }

    @Override
    public Object run() {
        //过滤器内容
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info("send {} request to {}",request.getMethod(),request.getRequestURI());
        Object accessToken = request.getParameter("accessToken");
        if(accessToken==null){
            log.warn("access token is null");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            return null;
        }
        log.info("access token is ok");
        return null;
    }
}
